package lines.gameengine.util;

import lines.gameengine.Ball;
import lines.gameengine.IBall;
import lines.gameengine.IField;
import lines.gameengine.strategy.IBallStrategyFactory;

import java.util.Collection;
import java.util.HashSet;
import java.util.Random;

/**
 * @author vpotapenko
 */
public class DefaultBallGenerator implements IBallGenerator {

    private final IBallStrategyFactory strategyFactory;
    private final Random random = new Random();

    public DefaultBallGenerator(IBallStrategyFactory strategyFactory) {
        this.strategyFactory = strategyFactory;
    }

    @Override
    public Collection<IBall> generateNextStepBalls(IField field) {
        Collection<IBall> nextBalls = new HashSet<IBall>();
        final int ballsPerStep = field.getContext().getParameters().getBallsPerStep();
        for (int i = 0; i < ballsPerStep; i++) {
            nextBalls.add(generateBall(field));
        }
        return nextBalls;
    }

    private IBall generateBall(IField field) {
        final int typesNumber = field.getContext().getParameters().getTypesNumber();
        int value = random.nextInt(typesNumber);
        return new Ball(strategyFactory.createBallStrategy(field, value));
    }
}
